const app = getApp();
const common = require('../../../utils/common.js');
const api = require('../../../config/api.js');

var pageNo = 1;
var hadLastPage = false;
var isLoad = 0;

Page({
  data: {
    imgUrl: app.globalData.imgurl,
		dataList: [],
    showNoData: false,
		showLoding: true,
  },
  onShow: function () {
		this.clearData();
		this.loadList();
  },
	clearData: function () {
		pageNo = 1;
		hadLastPage = false;
		this.setData({
			dataList: []
		});
	},
	loadList: function () {
		isLoad = 1;
		if (hadLastPage) {
			isLoad = 0;
			return;
		}
		var that = this;
		var data = {
			pageNo: pageNo
		}
		this.setData({
			showLoding: true,
			showNoData: false
		});
		setTimeout(function () {
			common.request(api.ProductCollectListData, data, 'POST').then(function (data) {
				that.setData({
					showLoding: false
				});
				if (data.type == 'success') {
					var dataList = that.data.dataList;
					for (var i = 0; i < data.data.dataList.length; i++) {
						dataList.push(data.data.dataList[i]);
					}
					if (data.data.pageNo == data.data.totalPages) {
						hadLastPage = true;
					} else {
						pageNo++;
					}
					that.setData({
						dataList: dataList
					});
					isLoad = 0;
					if (data.data.total == 0) {
						that.setData({
							showNoData: true
						});
					}
				} else {
					console.log(data.content);
					isLoad = 0;
				}
			});
		}, app.globalData.showLodingTime);
	},
	onReachBottom: function () {
		this.loadList();
	},
  openGoods(event) {
    let that = this;
		let index = event.currentTarget.dataset.index;
		let productId = this.data.dataList[index].productId;
		let productSkuId = this.data.dataList[index].productStockId;
    //触摸时间距离页面打开的毫秒数  
    var touchTime = that.data.touch_end - that.data.touch_start;
    console.log(touchTime);
    //如果按下时间大于350为长按  
    if (touchTime > 350) {
      wx.showModal({
        title: '',
        content: '确定删除吗？',
        success: function (res) {
          if (res.confirm) {
						var data = {
							isCollect: false,
							productId: productId,
							productStockId: productSkuId
						}
						common.request(api.ProductCollect, data, 'post').then(function (data) {
							if (data.type === 'success') {
								common.showSuccessToast('删除成功');
								var dataList = that.data.dataList;
								dataList.splice(index, 1);
								that.setData({
									dataList: dataList
								});
							} else {
								common.showErrorToast('删除失败');
							}
						});
          }
        }
      })
    } else {
      wx.navigateTo({
				url: '/pages/goods/goods?productSkuId=' + productSkuId
      });
    }  
  },
  //按下事件开始  
  touchStart: function (e) {
    let that = this;
    that.setData({
      touch_start: e.timeStamp
    })
    console.log(e.timeStamp + '- touch-start')
  },
  //按下事件结束  
  touchEnd: function (e) {
    let that = this;
    that.setData({
      touch_end: e.timeStamp
    })
    console.log(e.timeStamp + '- touch-end')
  }
})